<template>
    <div class="container">
        <div class="app-container">
            <!-- 结构 -->
            <div class="left">
                <el-input style="margin-bottom: 10px;" type="text" prefix-icon="el-icon-search" size="small"
                    @input="changeValue" v-model="queryParams.keyword" placeholder="输入员工姓名搜索" />
                <!-- 树形组件 -->
                <el-tree ref="deptTree" node-key='id' :data="depts" :props="defaultProps" default-expland-all
                    :expland-on-click-node="false" highlight-current @current-change="selectNode" />
            </div>
            <div class="right">
                <el-row class="opeate-tools" type="flex" justify="end">
                    <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
                    <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
                    <el-button size="mini" @click="exportEmployee">excel导出</el-button>
                </el-row>
                <!-- 表格组件 -->
                <el-table :data="list">
                    <!-- https://picsum.photos/ -->
                    <el-table-column prop="staffPhoto" align="center" label="头像">
                        <template v-slot="{ row }">
                            <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
                            <span v-else class="username">{{ row.username }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column prop="username" label="姓名" />
                    <el-table-column prop="mobile" label="手机号" sortable />
                    <el-table-column prop="workNumber" label="工号" sortable />

                    <el-table-column prop="fromOfEmployment" label="聘用方式">
                        <template v-slot="{ row }">
                            <span v-if="row.formOfEmployment == 1">正式</span>
                            <span v-else-if="row.formOfEmployment == 2">兼职</span>
                            <span v-else-if="row.formOfEmployment == 3">实习生</span>
                            <span v-else>无</span>
                        </template>
                    </el-table-column>

                    <el-table-column prop="departmentName" label="部门" />
                    <el-table-column prop="timeOfEntry" label="入职时间" sortable />
                    <el-table-column label="操作" width="280px">
                        <template v-slot="{row}">
                            <el-button size="mini" type="text">查看</el-button>
                            <el-button size="mini" type="text">角色</el-button>

                            <el-popconfirm title="确定删除该行数据吗？" @onConfirm="confirmDel(row.id)">
                                <el-button size="mini" style="margin-left: 10px;"  type="text" slot="reference">删除</el-button>
                            </el-popconfirm>

                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页 -->
                <el-row style="height: 60px;" align="middle" type="flex" justify="end">
                    <el-pagination layout="total,prev,pager,next" :total="total" :current-page="queryParams.page"
                        :page-size="queryParams.pageSize" @current-change="changePage" />
                </el-row>
            </div>
        </div>

        <!-- 放置导入的组件 -->
        <import-excel :show-excel-dialog.sync="showExcelDialog" @uploadSuccess='getEmployeeList' />

    </div>
</template>

<style lang="scss" scoped>
// sass ：css预编译语言
.app-container {
    background: #fff;
    display: flex;

    .left {
        width: 280px;
        padding: 20px;
        border-right: 1px solid #eaeef4;
    }

    .right {
        flex: 1;
        padding: 20px;

        .opeate-tools {
            margin: 10px;
        }

        .username {
            height: 30px;
            width: 30px;
            line-height: 30px;
            text-align: center;
            border-radius: 50%;
            color: #fff;
            background-color: #f5d02a;
            font-size: 12px;
            display: inline-block;
        }
    }
}

// .left{}
// .right{}
// .right .xxx{}
// .right .xxx  .sss{}

</style>

<script>
import { getDepartment } from '@/api/department'
import { delEmployee, exportEmployee, getEmployeeList } from '@/api/employee'
import { transList2TreeData } from '@/utils/index'
import FileSaver from 'file-saver'
import ImportExcel from './components/import-excel.vue'

export default {
    name: 'Employee',
    components: {
        // ImportExcel:ImportExcel
        ImportExcel
    },
    data() {
        return {
            showExcelDialog: false,//控制excel的弹出层显示和隐藏
            list: [],
            depts: [], // 组织架构
            defaultProps: {
                label: 'name',
                children: 'children'
            },
            // 存储查询参数
            queryParams: {
                departmentId: null,
                page: 1, //当前页码
                pagesize: 10, // 每页多少条
            },
            total: 0, // 记录员工总数
            keyword: '' // 模糊搜索关键词
        }
    },
    created() {
        this.getDepartment()
    },
    methods: {
        // 删除员工
        async confirmDel(id){
            await delEmployee(id)
            if(this.list.length === 1 && this.queryParams.page > 1) this.queryParams.page--
            this.getEmployeeList()
            this.$message.success('删除员工成功~')
        },
        // 导出员工
        async exportEmployee() {
            const result = await exportEmployee()
            // 下载文件
            FileSaver.saveAs(result, '员工信息表.xlsx')
        },
        async getDepartment() {
            let result = await getDepartment()
            // 转化树形
            this.depts = transList2TreeData(result.depts, 0)
            // 设置选中节点
            // 树形组件渲染是异步的，要等到更新完毕
            this.$nextTick(() => {
                // 意味着树渲染完毕
                this.$refs.deptTree.setCurrentKey(this.queryParams.departmentId)
            })

            // 根据记录的id参数 获取员工信息
            this.getEmployeeList()
        },
        // 获取员工列表方法
        async getEmployeeList() {
            const { rows, total } = await getEmployeeList(this.queryParams)
            this.list = rows
            this.total = total
        },
        // 切换部门
        selectNode(node) {
            this.queryParams.departmentId = node.id
            // 设置第一页
            this.queryParams.page = 1
            // 切换部门节点，重新获取员工
            this.getEmployeeList()
        },
        // 切换页码
        changePage(newPage) {
            // 新页码储存
            this.queryParams.page = newPage
            // 查询数据
            this.getEmployeeList()
        },
        // 模糊搜索
        changeValue() {
            // 单位时间内只执行最后一次
            clearTimeout(this.timer)
            this.timer = setTimeout(() => {
                this.queryParams.page = 1
                this.getEmployeeList()
            }, 300)
        }

    }
}
</script>